package com.angel.data.generator.interfaces.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.angel.common.helpers.StringHelper;
import com.angel.dao.generic.factory.DAOFactory;
import com.angel.data.generator.exceptions.DataGeneratorException;
import com.angel.data.generator.interfaces.DataGenerator;
import com.angel.io.separator.ColumnSeparator;

/**
 *
 * @author William
 *
 */
public abstract class FileDataGenerator implements DataGenerator{

	private static final long serialVersionUID = 4669673722778998329L;

	private String fullFilePath;

	public abstract void generateData(Collection<Object> dataCollection, DAOFactory daoFactory, List<String[]> rows);

    public FileDataGenerator(String fileName, String filePath){
        super();
        String _fullFilePath = (filePath != null ? filePath : "") + (fileName != null ? fileName:"");
        this.fullFilePath = _fullFilePath;
    }

    public FileDataGenerator(String fileName){
        this(fileName, System.getProperty("user.dir"));
    }

	public final void generateData(Collection<Object> dataCollection, DAOFactory daoFactory) {
		FileReader fr = null;
		try {
			fr = new FileReader(this.buildFile());
		    BufferedReader br = new BufferedReader(fr);
		    List<String[]> rows = new ArrayList<String[]>();
		    String row = null;
		    while ((row = br.readLine()) != null) {
		    	String[] fields = this.separatesFields(row);
		    	rows.add(fields);
		    }
		    this.generateData(dataCollection, daoFactory, rows);
		} catch (FileNotFoundException e) {
			throw new DataGeneratorException(e);
		} catch (IOException e) {
			throw new DataGeneratorException(e);
		} finally {
			try {
				if(fr != null){
					fr.close();
				}
			} catch (IOException e) {
				throw new DataGeneratorException("An error occurred closing file.", e);
			}
		}
	}

	private String[] separatesFields(String row) {
		List<String> objects = StringHelper.split(row, this.getColumnSeparator().getValue());
		String[] rows = new String[objects.size()];
		for(String object: objects){
			rows[objects.indexOf(object)] = object;
		}
		return rows;
	}

	private File buildFile(){
		File file = new File(this.getFullFilePath());
		if(file != null && file.isFile() && (!file.canRead() || !file.exists())){
			throw new DataGeneratorException("File [" + this.getFullFilePath() + "] couldn't be accesed.");
		}
		return file;
	}

	/**
	 * @return the fullFilePath
	 */
	public String getFullFilePath() {
		return fullFilePath;
	}

	/**
	 * @param fullFilePath the fullFilePath to set
	 */
	public void setFullFilePath(String fullFilePath) {
		this.fullFilePath = fullFilePath;
	}

	public ColumnSeparator getColumnSeparator(){
		return ColumnSeparator.COMMA;
	}
}
